<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.11.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>cppMISC: include/timer_old.hpp 源文件</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript" src="../../darkmode_toggle.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">cppMISC<span id="projectnumber">&#160;v1.1</span>
   </div>
   <div id="projectbrief">cppMisc 文档系统</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.11.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(1); });
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('../../',true,false,'search.php','搜索',false);
  $(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){ initResizable(false); });
/* @license-end */
</script>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">载入中...</div>
<div class="SRStatus" id="Searching">搜索中...</div>
<div class="SRStatus" id="NoMatches">未找到</div>
</div>
</div>
</div>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li>  </ul>
</div>
</div><!-- top -->
<div id="doc-content">
<div class="header">
  <div class="headertitle"><div class="title">timer_old.hpp</div></div>
</div><!--header-->
<div class="contents">
<a href="../../d0/de3/timer__old_8hpp.html">浏览该文件的文档.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span> </div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="preprocessor">#ifndef __TIMER_HPP__</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="preprocessor">#define __TIMER_HPP__</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span> </div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="preprocessor">#include &lt;functional&gt;</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span><span class="preprocessor">#include &lt;thread&gt;</span></div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span><span class="preprocessor">#include &lt;mutex&gt;</span></div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span><span class="preprocessor">#include &lt;atomic&gt;</span></div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span> </div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span><span class="comment">//#include &quot;defines.hpp&quot;</span></div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span><span class="preprocessor">#include &quot;<a class="code" href="../../d0/d0e/misc_8hpp.html">misc.hpp</a>&quot;</span></div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span> </div>
<div class="foldopen" id="foldopen00023" data-start="{" data-end="};">
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html">   23</a></span><span class="keyword">class </span><a class="code hl_class" href="../../db/de1/class_c_timer.html">CTimer</a></div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>{</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span><span class="keyword">private</span>:</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span>    std::atomic&lt;bool&gt;           m_is_one_shot;               <span class="comment">// 计时器是否是单次计时器</span></div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span>    std::atomic&lt;long&gt;           m_tick;                      <span class="comment">// 最大计时量</span></div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>    std::atomic&lt;bool&gt;           m_is_run;                    <span class="comment">// 计时线程是否正在运行</span></div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>    std::atomic&lt;bool&gt;           m_reset;                     <span class="comment">// 复位计时器</span></div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span> </div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span>    std::mutex                  m_mutex;</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span> </div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>    std::function&lt;void(  )&gt;     m_cb;                        <span class="comment">//要调用的事件发生的时候调用这个函数对象</span></div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span><span class="keyword">public</span>:</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#a93e4d770fd09f5e1b984768620cc52bf">   43</a></span>    <a class="code hl_function" href="../../db/de1/class_c_timer.html#a93e4d770fd09f5e1b984768620cc52bf">CTimer</a>(  ):m_is_one_shot( false ),m_tick( 0 ), m_is_run( false ) , m_reset( false ) {  }</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span> </div>
<div class="foldopen" id="foldopen00045" data-start="{" data-end="}">
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#a689ba0400ed26875987d51b5698e638f">   45</a></span>    <a class="code hl_function" href="../../db/de1/class_c_timer.html#a689ba0400ed26875987d51b5698e638f">CTimer</a>( <span class="keywordtype">long</span> ms , <span class="keywordtype">bool</span> oneShot = <span class="keyword">false</span> )</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>        :m_is_one_shot( oneShot ),m_tick( ms ) , m_is_run( false ) , m_reset( false ){}</div>
</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span> </div>
<div class="foldopen" id="foldopen00048" data-start="{" data-end="}">
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#ad4973e2b4c5c5dd14c3760bf7dfdd795">   48</a></span>    <a class="code hl_function" href="../../db/de1/class_c_timer.html#ad4973e2b4c5c5dd14c3760bf7dfdd795">CTimer</a>( std::function&lt;<span class="keywordtype">void</span>( )&gt; fun , <span class="keywordtype">long</span> ms , <span class="keywordtype">bool</span> oneShot = <span class="keyword">false</span> ):</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>        m_is_one_shot( oneShot ),m_tick( ms ) , m_is_run( false ) , m_reset( false ),m_cb( fun )</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>    {}</div>
</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span> </div>
<div class="foldopen" id="foldopen00052" data-start="{" data-end="}">
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#a9482f3b89043986caeb6136df6d4fd08">   52</a></span>    <span class="keyword">virtual</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#a9482f3b89043986caeb6136df6d4fd08">~CTimer</a>()</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>    {</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>        <a class="code hl_function" href="../../db/de1/class_c_timer.html#a7fe9ad152f641589251afdabebd60572">Stop</a>();</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>        std::this_thread::yield();</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>        std::this_thread::sleep_for( std::chrono::milliseconds( 10 ));</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>    }</div>
</div>
<div class="foldopen" id="foldopen00061" data-start="{" data-end="}">
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#ae550e92982e94eb1f9140212600bba2d">   61</a></span>    <span class="keyword">inline</span> <span class="keywordtype">long</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#ae550e92982e94eb1f9140212600bba2d">operator()</a>()<span class="keyword">const</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span><span class="keyword">    </span>{</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>        <span class="keywordflow">return</span> m_tick;</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    }</div>
</div>
<div class="foldopen" id="foldopen00065" data-start="{" data-end="}">
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#ad893a03dc5b05b3f1a204c0fd77658b1">   65</a></span>    <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#ad893a03dc5b05b3f1a204c0fd77658b1">operator()</a>( <span class="keywordtype">long</span> t )</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span>    { </div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>        m_tick = t; </div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>    }</div>
</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>    <span class="keywordtype">void</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#ad252c89444a7ed6eae41b09520015eda">Start</a>( <span class="keywordtype">long</span> ms = -1, <span class="keywordtype">bool</span> oneShot = <span class="keyword">false</span> );</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>    <span class="keywordtype">void</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#a46eb5cecd92e193d8b49f6b4454e480a">SetOwner</a>( std::function&lt;<span class="keywordtype">void</span>(  )&gt; fun );</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span>    <span class="keywordtype">void</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#a7fe9ad152f641589251afdabebd60572">Stop</a>(  );</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>    <span class="keywordtype">bool</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#ae948212119cc44200a991d76835b672c">IsRuning</a>(  );</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"><a class="line" href="../../db/de1/class_c_timer.html#aa9ce993463934cba250fa0214c07691b">   91</a></span>    <span class="keywordtype">void</span> <a class="code hl_function" href="../../db/de1/class_c_timer.html#aa9ce993463934cba250fa0214c07691b">Reset</a>(){ m_reset = true ; }</div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno">   92</span><span class="keyword">private</span>:</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno">   96</span>    <span class="keywordtype">void</span> thd_fun(  );</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno">   97</span>};</div>
</div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno">   98</span><span class="preprocessor">#endif</span></div>
<div class="ttc" id="aclass_c_timer_html"><div class="ttname"><a href="../../db/de1/class_c_timer.html">CTimer</a></div><div class="ttdoc">timer对象，在后台运行当计时触发条件成熟后触发运行指定函数。目前不支持带 参数的通知.这个对象实现全部采用标准的库实现，可以有比较好的通用型。但是要求 编译器支持c++11.</div><div class="ttdef"><b>定义</b> <a href="#l00023">timer_old.hpp:24</a></div></div>
<div class="ttc" id="aclass_c_timer_html_a46eb5cecd92e193d8b49f6b4454e480a"><div class="ttname"><a href="../../db/de1/class_c_timer.html#a46eb5cecd92e193d8b49f6b4454e480a">CTimer::SetOwner</a></div><div class="ttdeci">void SetOwner(std::function&lt; void()&gt; fun)</div><div class="ttdoc">指定事件响应函数，</div><div class="ttdef"><b>定义</b> <a href="../../d0/dc9/timer__old_8cpp_source.html#l00075">timer_old.cpp:75</a></div></div>
<div class="ttc" id="aclass_c_timer_html_a689ba0400ed26875987d51b5698e638f"><div class="ttname"><a href="../../db/de1/class_c_timer.html#a689ba0400ed26875987d51b5698e638f">CTimer::CTimer</a></div><div class="ttdeci">CTimer(long ms, bool oneShot=false)</div><div class="ttdef"><b>定义</b> <a href="#l00045">timer_old.hpp:45</a></div></div>
<div class="ttc" id="aclass_c_timer_html_a7fe9ad152f641589251afdabebd60572"><div class="ttname"><a href="../../db/de1/class_c_timer.html#a7fe9ad152f641589251afdabebd60572">CTimer::Stop</a></div><div class="ttdeci">void Stop()</div><div class="ttdoc">停止计时器</div><div class="ttdef"><b>定义</b> <a href="../../d0/dc9/timer__old_8cpp_source.html#l00007">timer_old.cpp:7</a></div></div>
<div class="ttc" id="aclass_c_timer_html_a93e4d770fd09f5e1b984768620cc52bf"><div class="ttname"><a href="../../db/de1/class_c_timer.html#a93e4d770fd09f5e1b984768620cc52bf">CTimer::CTimer</a></div><div class="ttdeci">CTimer()</div><div class="ttdoc">计时器的默认初始化函数，这个初始化函数没有指定回调函数，能够响应计时时间，需要调用SetOwner 指定事件触发的动作。使用CTimer( ms , oneShot ),...</div><div class="ttdef"><b>定义</b> <a href="#l00043">timer_old.hpp:43</a></div></div>
<div class="ttc" id="aclass_c_timer_html_a9482f3b89043986caeb6136df6d4fd08"><div class="ttname"><a href="../../db/de1/class_c_timer.html#a9482f3b89043986caeb6136df6d4fd08">CTimer::~CTimer</a></div><div class="ttdeci">virtual ~CTimer()</div><div class="ttdef"><b>定义</b> <a href="#l00052">timer_old.hpp:52</a></div></div>
<div class="ttc" id="aclass_c_timer_html_aa9ce993463934cba250fa0214c07691b"><div class="ttname"><a href="../../db/de1/class_c_timer.html#aa9ce993463934cba250fa0214c07691b">CTimer::Reset</a></div><div class="ttdeci">void Reset()</div><div class="ttdoc">复位计时，让计时器从零开始计时</div><div class="ttdef"><b>定义</b> <a href="#l00091">timer_old.hpp:91</a></div></div>
<div class="ttc" id="aclass_c_timer_html_ad252c89444a7ed6eae41b09520015eda"><div class="ttname"><a href="../../db/de1/class_c_timer.html#ad252c89444a7ed6eae41b09520015eda">CTimer::Start</a></div><div class="ttdeci">void Start(long ms=-1, bool oneShot=false)</div><div class="ttdoc">启动或者重启计时器。</div><div class="ttdef"><b>定义</b> <a href="../../d0/dc9/timer__old_8cpp_source.html#l00012">timer_old.cpp:12</a></div></div>
<div class="ttc" id="aclass_c_timer_html_ad4973e2b4c5c5dd14c3760bf7dfdd795"><div class="ttname"><a href="../../db/de1/class_c_timer.html#ad4973e2b4c5c5dd14c3760bf7dfdd795">CTimer::CTimer</a></div><div class="ttdeci">CTimer(std::function&lt; void()&gt; fun, long ms, bool oneShot=false)</div><div class="ttdef"><b>定义</b> <a href="#l00048">timer_old.hpp:48</a></div></div>
<div class="ttc" id="aclass_c_timer_html_ad893a03dc5b05b3f1a204c0fd77658b1"><div class="ttname"><a href="../../db/de1/class_c_timer.html#ad893a03dc5b05b3f1a204c0fd77658b1">CTimer::operator()</a></div><div class="ttdeci">void operator()(long t)</div><div class="ttdef"><b>定义</b> <a href="#l00065">timer_old.hpp:65</a></div></div>
<div class="ttc" id="aclass_c_timer_html_ae550e92982e94eb1f9140212600bba2d"><div class="ttname"><a href="../../db/de1/class_c_timer.html#ae550e92982e94eb1f9140212600bba2d">CTimer::operator()</a></div><div class="ttdeci">long operator()() const</div><div class="ttdoc">设置和获取计时器计时长度</div><div class="ttdef"><b>定义</b> <a href="#l00061">timer_old.hpp:61</a></div></div>
<div class="ttc" id="aclass_c_timer_html_ae948212119cc44200a991d76835b672c"><div class="ttname"><a href="../../db/de1/class_c_timer.html#ae948212119cc44200a991d76835b672c">CTimer::IsRuning</a></div><div class="ttdeci">bool IsRuning()</div><div class="ttdoc">判断计时器是否正在运行</div><div class="ttdef"><b>定义</b> <a href="../../d0/dc9/timer__old_8cpp_source.html#l00080">timer_old.cpp:80</a></div></div>
<div class="ttc" id="amisc_8hpp_html"><div class="ttname"><a href="../../d0/d0e/misc_8hpp.html">misc.hpp</a></div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.11.0
</small></address>
</div><!-- doc-content -->
</body>
</html>
